﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;


/**
 * Retrieves legal boy and girl names in Iceland
 * 
 * @author Ingi Gauti Ragnarsson
 */
namespace Is.Net {
	public class LegalNames {
		
		private List<string> GetNames(string url, int type) {

			MatchCollection mc;
			HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(url);
			HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
			Encoding enc = System.Text.Encoding.GetEncoding(1252);

			StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
			string lcHtml = loResponseStream.ReadToEnd();
			loWebResponse.Close();
			loResponseStream.Close();

			RegexOptions ro = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline;
			Regex re = new Regex("li>(<span.[^>]*>)?(.[^<]*)</(li|span)", ro);
			mc = re.Matches(lcHtml);


			var list = new List<string>();
			Match m;
			for (int i = 0; i < mc.Count; i++) {
				m = mc[i];
				list.Add(m.Groups[2].Value);
			}

			return list;
		}


		public List<string> GetBoyNamesApproved() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Drengir=on&Samthykkt=yes", 1);
		}

		public List<string> GetBoyNamesDeclined() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Drengir=on&Samthykkt=no", 2);
		}


		public List<string> GetGirlNamesApproved() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Stulkur=on&Samthykkt=yes", 3);
		}


		public List<string> GetGirlNamesDeclined() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Stulkur=on&Samthykkt=no", 4);
		}

		public List<string> GetMiddleNamesApproved() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Millinofn=on&Samthykkt=yes", 5);
		}

		public List<string> GetMiddleNamesDeclined() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=&Millinofn=on&Samthykkt=no", 6);
		}


		public List<string> GetNamesWithTHApproved() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=th&Stulkur=on&Drengir=on&Millinofn=on&Samthykkt=yes", 7);
		}

		public List<string> GetNamesWithTHDeclined() {
			return GetNames("http://www.rettarheimild.is/mannanofn?Stafrof=&Nafn=th&Stulkur=on&Drengir=on&Millinofn=on&Samthykkt=yes", 8);
		}


		public List<string> GetByLetterApproved(string letter, bool boys, bool girls, bool middle) {
			string girl = (girls) ? "on" : "off";
			string boy = (boys) ? "on" : "off";
			string m = (middle) ? "on" : "off";
			return GetNames("http://www.rettarheimild.is/mannanofn/?Stafrof=" + letter + "&Nafn=&Stulkur=" + girl + "&Drengir=" + boy + "&Millinofn=" + m + "&Samthykkt=yes", 0);
		}


		public List<string> GetByLetterDeclined(string letter, bool boys, bool girls, bool middle) {
			string girl = (girls) ? "on" : "off";
			string boy = (boys) ? "on" : "off";
			string m = (middle) ? "on" : "off";
			return GetNames("http://www.rettarheimild.is/mannanofn/?Stafrof=" + letter + "&Nafn=&Stulkur=" + girl + "&Drengir=" + boy + "&Millinofn=" + m + "&Samthykkt=no", 0);
		}


		public List<string> GetByNameApproved(string name, bool boys, bool girls, bool middle) {
			string girl = (girls) ? "on" : "off";
			string boy = (boys) ? "on" : "off";
			string m = (middle) ? "on" : "off";

			return GetNames("http://www.rettarheimild.is/mannanofn/?Stafrof=&Nafn=" + System.Web.HttpUtility.UrlEncode(name) + "&Stulkur=" + girl + "&Drengir=" + boy + "&Millinofn=" + m + "&Samthykkt=yes", 0);
		}
	}
}
